﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using WF_Business;

namespace ExtBusiness
{
    public class XtProcess
    {
        /// <summary>
        /// 根据流程ID取一个流程中的所有表格
        /// </summary>
        /// <param name="strName">流程ID</param>
        /// <returns></returns>
        /// <!--edit by zhongjian 20091027-->
        public DataTable GetProcess(string strFlowID)
        {
            DataTable dtTemp;

            //针对2.0版本
            string strSql = string.Format(@"select distinct t.res_value, t.path bm
              from st_static_resource t, st_workflow b, xt_workflow_define c
             where t.wid = b.wid
               and t.res_value is not null
               and b.rot = 0
               and b.wname = c.flowname and c.id='{0}' 
                and t.res_value not in (select fid from XT_PROCESS where flowid ='{0}')", strFlowID);

            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        /// <summary>
        /// 根据流程名获取相关内容
        /// </summary>
        /// <param name="strFlowID">流程ID</param>
        /// <returns></returns>
        /// <!--edit zhongjian 20091026-->
        public DataTable GetContent(string strFlowID)
        {
            DataTable dtTemp;
            string strSql = string.Format(@"select rownum,t.* from XT_PROCESS t 
                    where flowid = '{0}' order by orderdield ", strFlowID);

            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        /// <summary>
        /// 根据id获取相关内容
        /// </summary>
        /// <param name="strId">The STR id.</param>
        /// <returns></returns>
        public DataTable GetContentId(string strId)
        {

            DataTable dtTemp;
            string strSql = string.Format(@"select * from XT_PROCESS 
                    where ID = '{0}'", strId);

            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }


        /// <summary>
        /// 增加一个表单
        /// </summary>
        /// <param name="strFlowName">流程名</param>
        /// <param name="strTableName">表名</param>
        /// <param name="strTableNotherName">表别名</param>
        /// <param name="strFid">The STR fid.</param>
        /// <param name="strFlowID">流程ID</param>
        /// <!--editby zhongjian 20091021-->
        public static void InsertProcess(string strFlowName, string strTableName, 
            string strTableNotherName,string strFid,string strFlowID)
        {
            string strCount = UploadFileOperation.GetProcessMax(strFlowID);
            if (string.IsNullOrEmpty(strCount))
                strCount = "0";
            int intCount = int.Parse(strCount)+1;
            strCount = intCount.ToString();
            if (!string.IsNullOrEmpty(strFlowName))
            {
                string strSql = string.Format(@"Insert into XT_PROCESS (FLOWNAME,TABLENAME,
                    TABLEANOTHERNAME,fid,flowid,orderdield) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}')", strFlowName,
                    strTableName, strTableNotherName, strFid, strFlowID, strCount);

                SysParams.OAConnection().RunSql(strSql);
            }
        }

        /// <summary>
        /// 修改一个表单
        /// </summary>
        /// <param name="strId">The STR id.</param>
        /// <param name="strTableNotherName">Name of the STR table nother.</param>
        /// <param name="strTableName">Name of the STR table.</param>
        /// <param name="strFid">The STR fid.</param>
        /// <param name="strFlowID">流程ID</param>
        /// <!--editby zhongjian 20091021-->
        public static void UpdateIsFlag(string strId, string strTableNotherName,string strFlowID)
        {
            string strSql = string.Format(@"update XT_PROCESS set TABLEANOTHERNAME='{1}',flowid='{2}' where ID='{0}'",
                strId, strTableNotherName, strFlowID);

            SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 删除一个表单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static void DeleteProcess(string id)
        {
            string strSql = string.Format("delete from XT_PROCESS where id='{0}'", id);

            SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 获取表单数据字段内容列表
        /// </summary>
        /// <param name="strID">表单ID</param>
        /// <returns></returns>
        /// <!--addby zhongjian 20091119-->
        public DataTable GetProcessDataList(string strID)
        {
            DataTable dtTemp;
            string strSql = string.Format(@"select name,db_table,db_field 
                            from st_form_input where fid=(select fid from XT_PROCESS WHERE ID='{0}')", strID);
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        /// <summary>
        /// 获取用户表中字段内容列表
        /// </summary>
        /// <returns></returns>
        /// !--addby zhongjian 20091119-->
        public DataTable GetUserDataList()
        {
            DataTable dtTemp;
            string strSql = string.Format(@" select * from user_col_comments where table_name = 'SYS_USER'");
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        /// <summary>
        /// 获取用户表中字段内容列表
        /// </summary>
        /// <param name="strSouseTable">来源数据表名</param>
        /// <returns></returns>
        /// !--addby zhongjian 20091119-->
        public DataTable GetSouseDataList(string strSouseTable)
        {
            DataTable dtTemp;
            string strSql = string.Format(@" select * from user_col_comments where table_name = '{0}' AND COMMENTS IS NOT NULL", strSouseTable);
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        /// <summary>
        /// 获取部门表中字段内容列表
        /// </summary>
        /// <returns></returns>
        /// !--addby zhongjian 20091120-->
        public DataTable GetDepDataList()
        {
            DataTable dtTemp;
            string strSql = string.Format(@" select * from user_col_comments where table_name =  'ST_DEPARTMENT'
                            AND COMMENTS IS NOT NULL");
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        /// <summary>
        /// 添加初始化值设置内容
        /// </summary>
        /// <param name="strFlowName">流程名</param>
        /// <param name="strFlowID">对应xt_workflow_define表的id</param>
        /// <param name="strOtherName">表单别名</param>
        /// <param name="strSTableName">源表名</param>
        /// <param name="strSColName">源表列名</param>
        /// <param name="strSValue">源表列注释名</param>
        /// <param name="strTTableName">目标表名</param>
        /// <param name="strTColName">目标表列名</param>
        /// <param name="strTValue">目标表列注释名</param>
        /// <param name="strSqlData">构建提取数据SQL</param>
        /// <!--addby zhongjian 20091119-->
        public void InsertFormDefault(string strFlowName, string strFlowID,string strOtherName, string strSTableName, string strSColName, string strSValue,
            string strTTableName, string strTColName,string strTValue,string strSqlData)
        {
            string strSql = string.Empty;
            string strID = string.Empty;
            //判断该配置是否存在
            strSql = string.Format(@"select id from xt_form_default where flowid='{0}' and flowothername='{1}' and target_tabcol_name ='{2}' "
                                 , strFlowID, strOtherName,strTColName);
            strID= SysParams.OAConnection().GetValue(strSql);
            if (string.IsNullOrEmpty(strID))
            {
                //添加数据配置
                strSql = string.Format(@"insert into xt_form_default(flowname,flowid,flowothername,source_tab_name,source_tabcol_name,source_tabcol_comment,
                            target_tab_name,target_tabcol_name,target_tabcol_comment,LOGIC_SQL ) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')"
                    , strFlowName, strFlowID, strOtherName, strSTableName, strSColName, strSValue, strTTableName, strTColName, strTValue, strSqlData);
                SysParams.OAConnection().RunSql(strSql);
            }
            else
            {
                //修改数据配置
                strSql = string.Format(@"update xt_form_default set flowname='{0}',flowid='{1}',flowothername='{2}',source_tab_name='{3}',
                            source_tabcol_name='{4}',source_tabcol_comment='{5}',target_tab_name='{6}',target_tabcol_name='{7}',
                            target_tabcol_comment='{8}',LOGIC_SQL ='{9}'  where id='{10}'"
                    , strFlowName, strFlowID, strOtherName, strSTableName, strSColName, strSValue, strTTableName, strTColName, strTValue, strSqlData, strID);
                SysParams.OAConnection().RunSql(strSql);
            }
        }

        /// <summary>
        /// 获取已经配置的数据信息
        /// </summary>
        /// <param name="strOtherName">表单别名</param>
        /// <returns></returns>
        /// <!--addby zhongjian 20091125-->
        public DataTable GetDataList(string strOtherName, string strFlowName)
        {
            DataTable dtTemp;
            string strSql = string.Format(@" select id,flowname,flowid,flowothername,source_tab_name,source_tabcol_name,source_tabcol_comment,
                            target_tab_name,target_tabcol_name,target_tabcol_comment,LOGIC_SQL from xt_form_default 
                            where flowothername='{0}' and flowname='{1}'", strOtherName, strFlowName);
            SysParams.OAConnection().RunSql(strSql, out dtTemp);
            return dtTemp;
        }

        /// <summary>
        /// 删除表单数据配置
        /// </summary>
        /// <param name="strID">配置ID</param>
        /// <!--addby zhongjian 20091125-->
        public void DeleteData(string strID)
        {
            string strSql = string.Format(@"delete from xt_form_default where id='{0}'", strID);
            SysParams.OAConnection().RunSql(strSql);
        }
    }
}
